// +FHDR------------------------------------------------------------
//                 Copyright (c) 2022 .
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : r_monitor.sv
// Author        : 
// Created On    : 2022-08-25 15:47
// Last Modified : 
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------

`ifndef __R_MONITOR_SV__
`define __R_MONITOR_SV__

class r_monitor extends uvm_monitor;
	virtual r_interface vif;
	uvm_analysis_port #(r_transaction) ap;
	
	`uvm_component_utils(r_monitor)
	
	extern function new(string name = "r_monitor", uvm_component parent=null);
	extern virtual function void build_phase(uvm_phase phase);
	extern virtual task main_phase(uvm_phase phase);
	extern virtual task receive_pkt();
endclass: r_monitor

function r_monitor::new(string name = "r_monitor", uvm_component parent=null);
	super.new(name, parent);
endfunction: new

function void r_monitor::build_phase(uvm_phase phase);
	super.build_phase(phase);
	`uvm_info("r_monitor", "build_phase is called", UVM_HIGH);
	if(!uvm_config_db #(virtual r_interface)::get(this, "", "vif", vif))begin
		`uvm_fatal("r_monitor", "virtual interface get fatal");
	end
	ap   = new("ap", this);
endfunction: build_phase

task r_monitor::main_phase(uvm_phase phase);
	fork 
		this.receive_pkt();
	join_none
endtask: main_phase

task r_monitor::receive_pkt();
	r_transaction data = new();
	while(1) begin
		@this.vif.mon;
		if(this.vif.mon.valid & this.vif.mon.ready)begin
            data.data = this.vif.mon.data;
            data.last = this.vif.mon.last;
			data.unpack();
			ap.write(data);
		end
	end
endtask: receive_pkt

`endif
